package game;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;

import system.*;

public class HighScores extends Scene implements Runnable{

	AnimatedSprite background, background2, planet, title, window, error;
	TextHandler[] names;
	TextHandler[] scores;
	TextHandler loadingText;
	Actor bg1a, bg1b;

	Thread networkThread;

	final int ENTRIES = 10;
	int alphaIndex = 0;
	int fadeDelay = 0;
	
	boolean getScore = false;
	boolean isLoading = true;
	boolean updatedScores = false;
	
	String[] nameStrings;
	String[] scoreStrings;
	
	public HighScores(Game aGame, InputManager IM, SoundLibrary jukebox) {
		super(aGame, IM, jukebox);

		// fire up a network thread to read the score table
		networkThread = new Thread(this);
		networkThread.setName("networkThread");
		networkThread.start();

		// create the template for the scoreboard
		names = new TextHandler[ENTRIES];
		scores = new TextHandler[ENTRIES];
		for(int i=0; i<ENTRIES; i++){
			names[i] = new TextHandler(108, 67 + 16*i, "top", -1, 0, true, 14, 14);
			names[i].makeAlive(false);
			names[i].setText("");
			names[i].requireUpdate(true);
			names[i].update();
			
			scores[i] = new TextHandler(258, 67 + 16*i, "top", -1, 0, false, 14, 14);
			scores[i].makeAlive(false);
			scores[i].setText("");
			scores[i].requireUpdate(true);
			scores[i].update();
		}
		
		loadingText = new TextHandler(150, 130, "top", -1, 0, false, 14, 14);
		loadingText.makeAlive(true);
		loadingText.setText("LOADING");
		loadingText.requireUpdate(true);
		loadingText.update();
		
		try {
			background = new AnimatedSprite(new File("assets/sprites/scores-stars.png"), 480, 270, false, 0);
			background2 = new AnimatedSprite(new File("assets/sprites/scores-stars.png"), 480, 270, false, 0);
			planet = new AnimatedSprite(new File("assets/sprites/scores-overlay.png"), 480, 270, false, 0);
			title = new AnimatedSprite(new File("assets/sprites/scores-text.png"), 480, 270, false, 0);
			error = new AnimatedSprite(new File("assets/sprites/scores-error.png"), 210, 49, false, 0);
			window = new AnimatedSprite(new File("assets/sprites/scores-board.png"), 480, 270, false, 0);

			bg1a = new Actor("Parallax", background, 0, 0, -1, 0, 0);
			bg1b = new Actor("Parallax", background2, 480, 0, -1, 0, 0);

		} catch (IOException e) {
			System.out.println("Could not find sprites for high score screen");
		}

	}

	@Override
	public void paint(Graphics g){

		super.paint(g);

		Graphics2D g2d = (Graphics2D)g;

		g2d.setColor(new Color(2,3,12));
		g2d.fillRect(0, 0, 480, 270);

		g2d.drawImage(bg1a.getImage(),bg1a.getX(),bg1a.getY(), getGame());
		g2d.drawImage(bg1b.getImage(),bg1b.getX(),bg1b.getY(), getGame());

		g2d.drawImage(planet.getImage(), 0, 0, getGame());	

		g2d.drawImage(title.getImage(), 0, 0, getGame());

		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaIndex * 0.1f));
		g2d.drawImage(window.getImage(), 0, 0, getGame());

		for(int i=0; i<ENTRIES; i++){
			if(names[i].isAlive()){
				for(int j = 0; j < 10; j++){
					g.drawImage(names[i].getTextAt(j).getImage(), names[i].getTextAt(j).getX(), names[i].getTextAt(j).getY(), getGame());
				}
			}
			if(scores[i].isAlive()){
				for(int j = 0; j < 10; j++){
					g.drawImage(scores[i].getTextAt(j).getImage(), scores[i].getTextAt(j).getX(), scores[i].getTextAt(j).getY(), getGame());
				}
			}
		}
		if(loadingText.isAlive()){
			for(int j = 0; j < 10; j++){
				g.drawImage(loadingText.getTextAt(j).getImage(), loadingText.getTextAt(j).getX(), loadingText.getTextAt(j).getY(), getGame());
			}
		}
		
		if(!getScore && !isLoading){
			g2d.drawImage(error.getImage(), 130, 110, getGame());	
		}
		
		Toolkit.getDefaultToolkit().sync();
		g.dispose();

	}

	@Override
	public void update(){
		bg1a.update();
		bg1b.update();

		// update alpha index?
		if (alphaIndex < 10){
			alphaIndex++;
		}

		// return to TITLE
		if(inputs.isPressing(inputs.KEY_CANCEL)){
			jukebox.Stop("bgm-topscores.wav");
			jukebox.Play("bgm-title.wav");
			game.setState(Game.GAME_START);
			inputs.handledKey(inputs.KEY_CANCEL);
		}
		
		// successful load case -- update scores!
		if(getScore && !isLoading && !updatedScores){
			for(int i=0; i<ENTRIES; i++){
				names[i].makeAlive(true);
				names[i].setText(ScoreHandler.names[i]);
				names[i].requireUpdate(true);
				names[i].update();
				
				scores[i].makeAlive(true);
				scores[i].setText(ScoreHandler.scores[i]);
				scores[i].requireUpdate(true);
				scores[i].update();
			}
			updatedScores = true;
		}
		
		// check to display the LOADING text
		if(!updatedScores){
			loadingText.update();
		} else {
			loadingText.makeAlive(false);
		}

		// fail case: done loading and didn't get a score
		if (!getScore && !isLoading){
			loadingText.makeAlive(false);
		}
	}

	// thread execution to fetch the high score table
	@Override
	public void run() {
		isLoading = true;
		getScore = ScoreHandler.readScore();
		isLoading = false;
	}


}
